<#include "/templates/taglibs.ftl" />
<#include "/templates/function.ftl" />
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="${contextPath}/assets/plugins/layui/css/layui.css" media="all" />
        <style>
            p {
                margin-bottom: 10px;
                font-size: 15px;
                color: #393D49;
                font-weight: 400;
                line-height: 1.8;
            }
            .site-content {
                background-color: #fff;
                padding: 20px 0 10px 20px;
            }
            .site-h1 {
                margin-bottom: 20px;
                line-height: 60px;
                padding-bottom: 10px;
                color: #393D49;
                border-bottom: 1px solid #eee;
                font-size: 28px;
                font-weight: 400;
            }
        </style>
    </head>
    <body>
        <div class="layui-fluid layui-container">
            <div class="layui-row site-content">
                <div class="layui-col-md12">
                    <h1 class="site-h1">实体Entity</h1>
                    <p>项目开发中，entity（实体类）是必不可少的，它们一般都有很多的属性，并有相应的setter和getter方法。</p>
                    <p>entity（实体类）的作用一般是和数据表做映射。</p>
                    
                     <fieldset class="layui-elem-field layui-field-title site-title">
                        <legend><a name="get">规范说明</a></legend> 
                    </fieldset>
                    <p>1.实体类的名字尽量和数据库的表的名字对应相同。例如：user_info表，实体类：UserInfo。</p>
                    <p>在类名上添加注解@Entity和@Table(name = “user_info表”)</p>
                    <p>2.实体类应该实现java.io.Serializable或BaseEntity接口。</p>
                    <pre class="layui-code" lay-skin="notepad">
@Entity
@Table(name = "DEPARTMENT")
public class Department extends BaseEntity
                    </pre>
                    <p>3.根据你的设计，定义一组你需要的私有属性。根据这些属性，创建它们的setter和getter方法。（eclipse等集成开发软件可以自动生成)。</p>
                    <blockquote class="layui-elem-quote">
                        <p>实体中的id，建议定义为BigInt类型，使用 Snowflake 算法工具生成ID，例如：IdWorker.getInstance().nextId();</p>
                    </blockquote>
                    <p>4.实体类还应该有个属性serialVersionUID。例如：private static final long serialVersionUID = -6125297654796395674L;</p>
                    <p>5.在属性上适当的增加注解，对属性进行，例如：@Id、@Column</p>
                    <blockquote class="layui-elem-quote">
                        <p>实体中，常用的JPA注解有：</p>
                        <p>1.@Entity(name=”EntityName”)</p>
                        <p>表示该类是一个可持化的实体。当在容器中时，服务器将会首先加载所有标注了@Entity注释的实体类，其中@Entity中的“name”属性表示实体名称，若不作设置，默认为标注实体类的名称（注意大小写与系统的关系。）。 @Entity标注是必需的 ，name属性为可选。</p>
                        <p>2.@Table</p>
                        <p>在使用@Table标记时，需要注意以下几个问题。</p>
                        <p> (1) 此标记需要标注在类名前，不能标注在方法或属性前。</p>
                        <p> (2) name 属性表示实体所对应表的名称，默认表名为实体名称。</p>
                        <p> (3) catalog 和schema 属性表示实体指定的目录名或数据库名，这个根据不同的数据类型有所不同。</p>
                        <p>3.@Column</p>
                        <p>表示持久化属性映射表中的字段。此标记可以标注在Getter方法或属性前。</p>
                        <p>4.@Id</p>
                        <p>主键是实体的唯一标识，调用EntityManager的find方法，可以获得相应的实体对象。每一个实体类至少要有一个主键（Primary key）。</p>
                        <p>5.@Transient</p>
                        <p>作用是指定该属性或字段不是永久的。 即：如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient</p>
                    </blockquote>
                    
                     <fieldset class="layui-elem-field layui-field-title site-title">
                        <legend><a name="get">@Valid校验</a></legend> 
                    </fieldset>
                    <p>实体属性上，可以通过填加注解，实现对该属性的规则校验。在Controller中结合@Valid注解实现数据的验证。</p>
                    
                    <blockquote class="layui-elem-quote">
                        <p>@Valid 注解类型的使用:</p>
                        <p>1.@Null</p>
                        <p>限制只能为null</p>
                        <p>2.@NotNull</p>
                        <p>限制必须不为null</p>
                        <p>3.@AssertFalse</p>
                        <p>限制必须为false</p>
                        <p>4.@AssertTrue</p>
                        <p>限制必须为true</p>
                        <p>5.@DecimalMax(value)</p>
                        <p>限制必须为一个不大于指定值的数字</p>
                        <p>6.@DecimalMin(value)</p>
                        <p>限制必须为一个不小于指定值的数字</p>
                        <p>7.@Digits(integer,fraction)</p>
                        <p>限制必须为一个小数，且整数部分的位数不能超过integer，小数部分的位数不能超过fraction</p>
                        <p>8.@Future</p>
                        <p>限制必须是一个将来的日期</p>
                        <p>9.@Max(value)</p>
                        <p>限制必须为一个不大于指定值的数字</p>
                        <p>10.@Min(value)</p>
                        <p>限制必须为一个不小于指定值的数字</p>
                        <p>11.@Past</p>
                        <p>限制必须是一个过去的日期</p>
                        <p>12.@Pattern(value)</p>
                        <p>限制必须符合指定的正则表达式</p>
                        <p>13.@Size(max,min)</p>
                        <p>限制字符长度必须在min到max之间</p>
                        <p>14.@NotEmpty</p>
                        <p>验证注解的元素值不为null且不为空（字符串长度不为0、集合大小不为0）</p>
                        <p>15.@NotBlank</p>
                        <p>验证注解的元素值不为空（不为null、去除首位空格后长度为0），不同于@NotEmpty，@NotBlank只应用于字符串且在比较时会去除字符串的空格</p>
                        <p>16.@Email</p>
                        <p>验证注解的元素值是Email，也可以通过正则表达式和flag指定自定义的email格式</p>
                        <p>17.自定义校验类型</p>
                        
                    </blockquote>
                    
                    <fieldset class="layui-elem-field layui-field-title site-title">
                        <legend><a name="get">Json序列化输出</a></legend> 
                    </fieldset>
                    <p>实体在为前端做Json结构数据序列化输出时，需要对有些属性进行格式化输出，可以通过@JsonSerialize注解进行格式转换。</p>
                    <p>先定义格式，写好转换规则，再在实体类中要转换的属性上添加注解。</p>
                    
                    <pre class="layui-code" lay-skin="notepad">
package org.stvd.core.annotation.serializer;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
public class DateTimeSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = formatter.format(value);
        jgen.writeString(formattedDate);
    }
}
                    </pre>
                    <pre class="layui-code" lay-skin="notepad">
@Column(name="INDATE")    
@JsonSerialize(using = DateTimeSerializer.class)
public Date getIndate() {
    return indate;
}
                    </pre>
                    
                    <fieldset class="layui-elem-field layui-field-title site-title">
                        <legend><a name="get">完整代码实例</a></legend> 
                    </fieldset>
                    <pre class="layui-code" lay-skin="notepad">
package org.stvd.entities.admin;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotEmpty;
import org.stvd.core.annotation.serializer.DateTimeSerializer;
import org.stvd.entities.base.BaseEntity;
@Entity
@Table(name="USERS")
public class Users extends BaseEntity{
    /**
     * 
     */
    private static final long serialVersionUID = 3205348626676688839L;
    private String userid = "";
    private String uname = "" ;
    private String pwd;
    private String pwdConfirm = "" ;
    private String oldPwd = "" ;
    private String enable;
    private Date indate ;
    private Date modifydate ;
    private Date lastdate ;
    private Integer logintimes=0;
    private List<Roles> roleList=new ArrayList<Roles>();
    private List<Department> departmentList=new ArrayList<Department>();
    private List<UserLogin> userLoginList=new ArrayList<UserLogin>();
    private String role_id = "" ;
    private String dep_id = "";
    @Id
    @Column(name="USERID",nullable=false)
    @NotEmpty(message="用户ID不能为空！")
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    @Column(name="UNAME",nullable=false)
    @NotEmpty(message="用户名称不能为空！")
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    @Column(name="PWD",nullable=false)
    @NotEmpty(message="密码不能为空！")
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Transient
    public String getPwdConfirm() {
        return pwdConfirm;
    }
    public void setPwdConfirm(String pwdConfirm) {
        this.pwdConfirm = pwdConfirm;
    }
    @Transient
    public String getOldPwd() {
        return oldPwd;
    }
    public void setOldPwd(String oldPwd) {
        this.oldPwd = oldPwd;
    }
    @Column(name="ENABLE",nullable=false)
    public String getEnable() {
        return enable;
    }
    public void setEnable(String enable) {
        this.enable = enable;
    }
    @Column(name="INDATE")    
    @JsonSerialize(using = DateTimeSerializer.class)
    public Date getIndate() {
        return indate;
    }
    public void setIndate(Date indate) {
        this.indate = indate;
    }
    @Column(name="MODIFYDATE")
    public Date getModifydate() {
        return modifydate;
    }
    public void setModifydate(Date modifydate) {
        this.modifydate = modifydate;
    }
    @Column(name="LASTDATE")    
    @JsonSerialize(using = DateTimeSerializer.class)
    public Date getLastdate() {
        return lastdate;
    }
    public void setLastdate(Date lastdate) {
        this.lastdate = lastdate;
    }
    @Column(name="LOGINTIMES")    
    public Integer getLogintimes() {
        return logintimes;
    }
    public void setLogintimes(Integer logintimes) {
        this.logintimes = logintimes;
    }
    @OneToMany(cascade = {CascadeType.REFRESH},fetch=FetchType.EAGER)
    @JoinTable(name = "USER_ROLE",
            joinColumns = {
                @JoinColumn(name="USERID")
            },
            inverseJoinColumns = @JoinColumn(name = "ROLEID")
       ) 
    @Fetch(FetchMode.SUBSELECT)
    public List<Roles> getRoleList() {
        return roleList;
    }
    public void setRoleList(List<Roles> roleList) {
        this.roleList = roleList;
    }
    @OneToMany(cascade = {CascadeType.REFRESH},fetch=FetchType.EAGER)
    @JoinTable(name = "USER_DEP",
            joinColumns = {
                @JoinColumn(name="USERID")
            },
            inverseJoinColumns = @JoinColumn(name = "DEP_ID")
       ) 
    @Fetch(FetchMode.SUBSELECT)
    public List<Department> getDepartmentList() {
        return departmentList;
    }
    public void setDepartmentList(List<Department> departmentList) {
        this.departmentList = departmentList;
    }
    @Transient
    public List<UserLogin> getUserLoginList() {
        return userLoginList;
    }
    public void setUserLoginList(List<UserLogin> userLoginList) {
        this.userLoginList = userLoginList;
    }
    @Transient
    public String getDep_id() {
        if(dep_id != null && !dep_id.equals(""))
            return dep_id;
        for(Department department : departmentList){
            if(dep_id == null || dep_id.equals("")){
                dep_id = department.getDep_id();
            } else {
                dep_id += "," + department.getDep_id();
            }
        }
        return dep_id;
    }
    public void setDep_id(String dep_id) {
        this.dep_id = dep_id;
    }
    @Transient
    public String getRole_id() {
        if(role_id != null && !"".equals(role_id))
            return role_id;
        for(Roles role : roleList){
            if("".equals(role_id)){
                role_id = role.getRoleid();
            } else {
                role_id += "," + role.getRoleid();
            }
        }
        return role_id;
    }
    public void setRole_id(String role_id) {
        this.role_id = role_id;
    }
    @Transient
    public String getRoles() {
        String roleNames = "" ;
        for(Roles role : roleList){
            if("".equals(roleNames)){
                roleNames = role.getRname();
            } else {
                roleNames += "," + role.getRname();
            }
        }
        return roleNames;
    }
    @Transient
    public String getDeps() {
        String depNames = "" ;
        for(Department department : departmentList){
            if("".equals(depNames)){
                depNames = department.getDep_name();
            } else {
                depNames += "," + department.getDep_name();
            }
        }
        return depNames;
    }    
}
                    </pre>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript" src="${contextPath}/assets/plugins/layui/layui.js"></script>
    <script>
        layui.use('code', function(){ 
            layui.code({
                encode: true
            }); 
          });
    </script>
</html>